/*
============================================================================
 Name			: BlueToothConnector.cpp
 Author	  		: 
 Copyright   	:
 Description 	: Implementation
============================================================================
*/

// INCLUDE FILES

#include "BluetoothSockConnector.h"
#include "common.hrh"
/*
============================================================================
CBluetoothSockConnector's two stage constructor
============================================================================
*/
CBluetoothSockConnector* CBluetoothSockConnector::NewL(RSocket& aBtSocket, MSocketConnectorObserver& aSockConnObs, RSocketServ& aSocketServer)
	{
    CBluetoothSockConnector* self = new (ELeave) CBluetoothSockConnector(aBtSocket, aSockConnObs, aSocketServer);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop();
    return self;
	}

/*
============================================================================
CBluetoothSockConnector's costructor
============================================================================
*/
CBluetoothSockConnector::CBluetoothSockConnector(RSocket& aBtSocket, MSocketConnectorObserver& aSockConnObs, RSocketServ& aSocketServer)
	:CActive(CActive::EPriorityStandard), iBtSocket(aBtSocket), iSockConnObs(aSockConnObs), iSocketServer(aSocketServer)
	{
	CActiveScheduler::Add(this);
	}

/*
============================================================================
CBluetoothSockConnector's second phase constructor 
============================================================================
*/
void CBluetoothSockConnector::ConstructL()
	{	
	/*
	============================================================================
	Using the Socket Server we need to load the RFCOMM communication protocol.
	First of all we need to see if BT RFCOMM is supported.
	============================================================================
	*/
	TProtocolDesc pdesc;
	User::LeaveIfError(iSocketServer.FindProtocol(KBTRFCOMM(), pdesc));

	/*
	============================================================================
	Open the socket using BT RFCOMM
	============================================================================
	*/
	User::LeaveIfError(iBtSocket.Open(iSocketServer, KBTRFCOMM));
	}

/*
============================================================================
CBluetoothSockConnector's destructor
============================================================================
*/
CBluetoothSockConnector::~CBluetoothSockConnector()
	{
	Cancel();
	}

/*
============================================================================
DoCancel is called as part of the active object's Cancel().
Outstanding operations for a BT socket include: read, write, Ioctl, connect,
accept, shutdown and the Baseband event notifier. All of these operations will
be completed by this call
============================================================================
*/
void CBluetoothSockConnector::DoCancel()
	{
	iBtSocket.CancelAll();
	}

/*
============================================================================
Handles the active object's socket connection completion event 
============================================================================
*/
void CBluetoothSockConnector::RunL()
	{
	iSockConnObs.OnSockConnectionComplete(iStatus.Int());
	}

/*
============================================================================
Connect to the remote BT device on a given BT device address and on a given RFCOMM service's port
============================================================================
*/
void CBluetoothSockConnector::ConnectToRemoteBtDeviceL(const TBTDevAddr& aDevAddr, TInt aPort)
	{
	/*
	============================================================================
	Set the remote device address and service port using TBTSockAddr
	============================================================================
	*/
	iBtDevAddr.SetBTAddr(aDevAddr);
	iBtDevAddr.SetPort(aPort);
	
	/*
	============================================================================
	Connect the BT socket to the remote device.
	============================================================================
	*/
	iBtSocket.Connect(iBtDevAddr, iStatus);
	SetActive();
	}
	
/*
============================================================================
Handles a leave occurring in the request completion event handler RunL().
============================================================================
*/
TInt CBluetoothSockConnector::RunError(TInt aError)
	{
	iSockConnObs.OnSockConnectionComplete(aError);
	return KErrNone;
	}
